using System;
using System.Linq;
using Hayabusa.Database;

namespace Hayabusa.Shell
{
    public class Admin
    {
        public void Init()
        {
            using (DbContextDataContext db = new DbContextDataContext())
            {
                var adminExist = from u in db.Utilisateurs
                                 where u.Login == "admin" 
                                    && u.Password == "admin"
                                 select u;

                if(adminExist.Count() == 0)
                {
                    db.Utilisateurs.InsertOnSubmit(new Utilisateurs
                    {
                        Id = Guid.NewGuid(),
                        Login = "admin",
                        Password = "admin",
                        Role = "Admin"
                    });
                    db.SubmitChanges();
                }
            }
        }

        public string Check(string user, string password)
        {
            using (DbContextDataContext db = new DbContextDataContext())
            {
                var checkedRole = from u in db.Utilisateurs
                                  where u.Login == user
                                        && u.Password == password
                                  select u;
                return checkedRole.Count() != 0 ? checkedRole.First().Role : "interdit";
            }
        }
    }
}